package com.coders.activities;

import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCTextureCache;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.sound.SoundEngine;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.coders.R;
import com.coders.configuration.SettingsConfig;
import com.coders.layer.PlayLayer;

/**
 * Entry point for game module.
 * 
 * @author subin
 * @see PlayLayer
 */
public class Play extends BaseActivity
{
    protected CCGLSurfaceView glSurfaceView;
    private static boolean    isResumeGame = false;
    CCScene                   scene;
    int                       isStart;

    @Override
    public void onCreate( Bundle savedInstanceState )
    {

        super.onCreate( savedInstanceState );
        glSurfaceView = new CCGLSurfaceView( this );

        setContentView( glSurfaceView );
        PlayLayer.setHelpShown( true );

    }

    public static boolean isResumeGame()
    {
        return isResumeGame;
    }

    public static void setResumeGame( boolean isResumeGame )
    {
        Play.isResumeGame = isResumeGame;

    }

    @Override
    public void onStart()
    {
        super.onStart();
        SoundEngine.sharedEngine().preloadEffect( CCDirector.sharedDirector().getActivity(), R.raw.background );

        startBackgroundMusic();

        SoundEngine.sharedEngine().preloadEffect( CCDirector.sharedDirector().getActivity(), R.raw.balloonblastsound );

        SoundEngine.sharedEngine().preloadEffect( CCDirector.sharedDirector().getActivity(), R.raw.paisleyballoon );
        SoundEngine.sharedEngine().preloadEffect( CCDirector.sharedDirector().getActivity(), R.raw.devilsound );

        CCDirector.sharedDirector().attachInView( glSurfaceView );
        CCDirector.sharedDirector().setDeviceOrientation( CCDirector.kCCDeviceOrientationLandscapeLeft );
        CCDirector.sharedDirector().setDisplayFPS( true );
        CCDirector.sharedDirector().setAnimationInterval( 1.0f / 60.0f );

        if ( SettingsConfig.getSelectedPop() == "BALOONS" )
        {
            scene = PlayLayer.scene();
        }
        else
        {

        }
        CCDirector.sharedDirector().runWithScene( scene );

        CCDirector.sharedDirector().pause();
    }

    private void startBackgroundMusic()
    {

        if ( SettingsConfig.isMusicEnabled() )
        {
            SoundEngine.sharedEngine().playSound( CCDirector.sharedDirector().getActivity(), R.raw.background, true );
        }

    }

    @Override
    public void onPause()
    {
        super.onPause();
        SoundEngine.sharedEngine().pauseSound();
        CCDirector.sharedDirector().pause();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        SoundEngine.sharedEngine().resumeSound();
        CCDirector.sharedDirector().resume();

    }

    @Override
    public void onStop()
    {
        super.onStop();
        CCDirector.sharedDirector().end();
        isResumeGame = true;
        Play.setResumeGame( false );
        PlayLayer.setHelpShown( false );

    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();

        CCDirector.sharedDirector().end();
        CCTextureCache.sharedTextureCache().removeAllTextures();
        SoundEngine.sharedEngine().realesAllSounds();
        SoundEngine.sharedEngine().realesAllEffects();
        SoundEngine.purgeSharedEngine();
    }

    public boolean onKeyDown( int keyCode, KeyEvent event )
    {
        if ( keyCode == KeyEvent.KEYCODE_BACK )
        {
            // showExitGameDialog();
            CCDirector.sharedDirector().end();
            CCTextureCache.sharedTextureCache().removeAllTextures();
            SoundEngine.sharedEngine().realesAllSounds();
            SoundEngine.sharedEngine().realesAllEffects();
            SoundEngine.purgeSharedEngine();
            Intent mainIntent = new Intent( Play.this, MainMenu.class );
            Play.this.startActivity( mainIntent );
            finish();

        }
        return super.onKeyDown( keyCode, event );
    }

    public void showExitGameDialog()
    {
        AlertDialog.Builder localBuilder = new AlertDialog.Builder( this );
        localBuilder.setMessage( "Are you sure to quit game?" );
        localBuilder.setCancelable( false );
        localBuilder.setPositiveButton( "Quit", new DialogInterface.OnClickListener()
        {
            public void onClick( DialogInterface paramDialogInterface, int paramInt )
            {
                Intent intent = new Intent();
                setResult( RESULT_CANCELED, intent );
                finish();

                CCDirector.sharedDirector().end();
                CCTextureCache.sharedTextureCache().removeAllTextures();
                SoundEngine.sharedEngine().realesAllSounds();
                SoundEngine.sharedEngine().realesAllEffects();
                SoundEngine.purgeSharedEngine();
                Intent mainIntent = new Intent( Play.this, MainMenu.class );
                Play.this.startActivity( mainIntent );
                finish();

            }
        } );
        localBuilder.setNegativeButton( "Continue", new DialogInterface.OnClickListener()
        {
            public void onClick( DialogInterface paramDialogInterface, int paramInt )
            {
                paramDialogInterface.cancel();
                CCDirector.sharedDirector().onResume();
            }
        } );
        localBuilder.show();
    }
}
